package lambda;

import lambda.entity.User;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author LIBOFEI
 * @ClassName Test9
 * @description
 * @date 2021/11/30 14:28
 * @Version 1.0
 **/
public class Test9 {

    public static void main(String[] args) {

        //获取String集合中最长的元素。
        List<String> list = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");

        Optional<String> max = list.stream().max(Comparator.comparing(String::length));
        System.out.println("最长的字符串：" + max.get());


        //获取Integer集合中的最大值。
        List<Integer> list2 = Arrays.asList(7, 6, 9, 4, 11, 6);

        // 自然排序
        Optional<Integer> max1 = list2.stream().max(Integer::compareTo);
        // 自定义排序
        Optional<Integer> max2 = list2.stream().max(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        });
        System.out.println("自然排序的最大值：" + max1.get());
        System.out.println("自定义排序的最大值：" + max2.get());




        List<User> UserList = new ArrayList<User>();
        UserList.add(new User(23, "male1"));
        UserList.add(new User( 25, "male2"));
        UserList.add(new User( 21, "female1"));
        UserList.add(new User( 24, "female2"));
        UserList.add(new User( 25, "male3"));
        UserList.add(new User(26, "female3"));
        Optional<User> max3 = UserList.stream().max(Comparator.comparingInt(User::getId));
        System.out.println("id最大的id：" + max3.get().getId());



        List<Integer> list3 = Arrays.asList(7, 6, 4, 8, 2, 11, 9);
        long count = list3.stream().filter(x -> x > 6).count();
        System.out.println("list中大于6的元素个数：" + count);

    }
}
